package net.csdn.business.webhook.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.csdn.business.common.domain.query.WebhookConfigEventQuery;
import net.csdn.business.common.domain.request.webhook.NewWebhookConfigEventDTO;
import net.csdn.business.common.domain.vo.WebhookConfigEventVO;
import net.csdn.business.webhook.entity.WebhookConfigEvent;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


/**
 * @ClassName WebhookConfigEventMapper
 * @Description webhook配置事件Mapper接口
 * @Author zhangyl
 * @Date 2023/4/23 17:21
 */
public interface WebhookConfigEventMapper extends BaseMapper<WebhookConfigEvent>{


    /**
     * 查询webhook配置事件列表
     * 
     * @param webHookId webhook配置事件
     * @return webhook配置事件集合
     */
    @Select("select t1.trigger_event FROM webhook_config_event t1 where t1.web_hook_id=#{webHookId} order by t1.created_date asc")
    public List<String> selectWebhookConfigEventList(@Param("webHookId") String webHookId);

    /**
     * 批量查询webhook配置事件列表
     *
     * @param list webhook配置的id集合
     * @return webhook配置事件集合
     */
    @Select("<script>select t1.web_hook_id,group_concat(t1.trigger_event) FROM webhook_config_event t1 where t1.web_hook_id in" +
            "        <foreach collection='list' item='item' index='index' separator=','> " +
            "             #{item} " +
            "        </foreach>" +
            "GROUP BY t1.web_hook_id</script>")
    public Map<String,String> selectBatchWebhookConfigEvent(@Param("list") List<String> list);

    /**
     * 批量保存webhook配置事件
     *
     * @param list webhook配置事件列表
     * @return 结果
     */
    @Insert("<script>" +
            "insert into webhook_config_event (object_id,web_hook_id,trigger_event,created_by,created_date\n" +
            "      )\n" +
            "        values\n" +
            "        <foreach collection='list' item='event' index='index' separator=','>\n" +
            "            ( #{event.objectId}, #{event.webHookId}, #{event.triggerEvent}, #{event.createdBy}, now() \n" +
            "            )\n" +
            "        </foreach>" +
            "</script>")
    public Integer insertBatch(@Param("list")List<WebhookConfigEvent> list);


    /**
     * 根据webhook配置id删除webhook配置事件
     * 
     * @param webHookId webhook配置事件id
     * @return 结果
     */
    @Delete("DELETE FROM webhook_config_event where web_hook_id=#{webHookId}")
    public Integer deleteWebhookEventByWebHookId(@Param("webHookId")String webHookId);

}
